所謂 CRUD => Create , Read , Update , Delete 指的就是增刪改查
,為資料庫一個很重要的地方。
前面我們做過C CREATE,R SELECT了,接下來要來操輟關於UPDATE、DELETE的部分。
透過,可以一次改變多個column。
UPDATE table_name set salary=8700,notes=updated where title="Software Engineer"
+----+------------+-----------+-------------------+--------+------------+-------+
| id | first_name | last_name | title | salary | hire_date | notes |
+----+------------+-----------+-------------------+--------+------------+-------+
| 1 | Robin | Jackman | Software Engineer | 8700 | 2001-10-12 | updated |
| 5 | Eliza | Clifford | Software Engineer | 8700 | 1998-10-19 | updated |
| 6 | Nancy | Newman | Software Engineer | 8700 | 2007-01-23 | updated |
+----+------------+-----------+-------------------+--------+------------+-------+
DELETE from employee where title="Software Engineer";
uery OK, 3 rows affected (0.01 sec)
可發現其資料已經消失。
mysql> SELECT * from employee;
+----+------------+-----------+------------------------+--------+------------+-------+
| id | first_name | last_name | title | salary | hire_date | notes |
+----+------------+-----------+------------------------+--------+------------+-------+
| 2 | Taylor | Edward | Software Architect | 7200 | 2002-09-21 | NULL |
| 3 | Vivian | Dickens | Database Administrator | 6000 | 2012-08-29 | NULL |
| 4 | Harry | Clifford | Database Administrator | 6800 | 2015-12-10 | NULL |
| 7 | Melinda | Clifford | Project Manager | 8500 | 2013-10-29 | NULL |
| 8 | Jack | Chan | Test Engineer | 6500 | 2018-09-07 | NULL |
| 9 | Harley | Gilbert | Software Architect | 8000 | 2000-07-17 | NULL |
+----+------------+-----------+------------------------+--------+------------+-------+
6 rows in set (0.00 sec)
DELETE from table_name
可以刪除指定之table所有的資料。
原本我們不加上IF NOT EXISTS雖然還是有執行INSERT的部分,但還是會報錯,而加上以後,就沒有報錯INSERT的部分也有執行,整個代碼會更加嚴謹。